<?php
namespace Home\Controller;
class MineController extends HomeController {

	public function _initialize(){
		parent::_initialize();
        !is_login() && $this->error('请先登录', U('login/index'));
	}
	public function index(){
		$userinfo = M('member')->field('uid,nickname,avatar,amount')->where(array('uid'=>UID))->find();
		$userinfo['user_avatar'] = get_cover($userinfo['avatar'],'path');
		if (empty($userinfo['user_avatar'])) {
			$userinfo['user_avatar'] = get_url($userinfo['avatar']);
		}
		$win_num = M('user_code')->where(array('uid'=>UID,'status'=>2))->count();
		$this->assign('userinfo',$userinfo);
		$this->assign('win_num',$win_num);
		$this->display();
	}
	/**
	 * 我的钱包
	 * @return [type] [description]
	 */
	public function mine_wallet(){
		$user_amount = M('member')->where(array('uid'=>UID))->getField('amount');
		$map = array(
			'uid'	=>	UID,
			'pur_type'	=>	1
		);
		$list = $this->lists("user_trade",$map);
		int_to_string($list,array('type'=>C('trade_type')));
		$this->assign("list",$list);
		$this->assign("user_amount",$user_amount);
		$this->display();
	}
	/**
	 * 我的播豆
	 * @return [type] [description]
	 */
	public function mine_coin(){
		$user_coin = M('users',"cmf_")->where("id='".UID."'")->getField('coin');
		if (empty($user_coin)) {
			$user_coin = 0;
		}
		$map = array(
			'uid'	=>	UID,
			'pur_type'	=>	2
		);
		$list = $this->lists("user_trade",$map);
		int_to_string($list,array('type'=>C('trade_type')));
		$this->assign("list",$list);
		$this->assign("user_coin",$user_coin);
		$this->display();
	}
	/**
	 * 中奖记录
	 * @return [type] [description]
	 */
	public function mine_win(){
		$win_type = I("win_type",'');
		$snatch_ids = array();
		$win_list = M("user_code")->field('snatch_id')->where(array('uid'=>UID,'status'=>2))->select();
		$win_count = count($win_list);
		if (!empty($win_list)) {
			foreach ($win_list as $key => $value) {
				$snatch_ids[] = $value['snatch_id'];
			}
		}
		$map = array(
			'id'	=>	array('in',$snatch_ids),
			'status'	=>	2
		);
		if ($win_type == 'coin') {
			$map['prize'] = 'coin';
		}
		if ($win_type == 'cash') {
			$map['prize'] = 'cash';
		}
		$list = $this->lists("snatch",$map);
		$this->assign('list',$list);
		$this->assign('win_type',$win_type);
		$this->assign('win_count',$win_count);
		$this->display();
	}
	/**
	 * 参与记录
	 * @return [type] [description]
	 */
	public function mine_join(){
		$snatch_status = I("snatch_status",0);
		$snatch_ids = array();
		$join_list = M("user_code")->field("snatch_id")->where(array('uid'=>UID))->select();
		if (!empty($join_list)) {
			foreach ($join_list as $key => $value) {
				$snatch_ids[] = $value['snatch_id'];
			}
		}
		$map = array(
			'id'	=>	array('in',$snatch_ids)
		);
		if ($snatch_status > 0) {
			$map['status']	= $snatch_status;
		}
		$list = $this->lists("snatch",$map);
		$this->assign('list',$list);
		$this->assign('snatch_status',$snatch_status);
		$this->display();
	}
	/**
	 * 历史发布记录
	 * @return [type] [description]
	 */
	public function mine_history(){
		$snatch_status = I("snatch_status",0);
		$map = array(
			'uid'	=>	UID
		);
		if ($snatch_status > 0) {
			$map['status']	= $snatch_status;
		}
		$list = $this->lists("snatch",$map);
		$this->assign('list',$list);
		$this->assign('snatch_status',$snatch_status);
		$this->display();
	}
	/**
	 * 更新头像
	 * @return [type] [description]
	 */
	public function updateAvator(){
		if (!is_login()) {
			$this->error("请先登录",U('login/index'));
		}
		if (empty($_FILES['avator']['name'])) {
			$this->error("请上传图片");
		}
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Picture->upload(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        );
        if ($info) {
        	$cover_id = $info['avator']['id'];
        }else{
        	$this->error("上传图片失败！");
        }
		$result = M('member')->where(array('uid'=>UID))->setfield('avatar', $cover_id);
		if ($result !== false) {
			//修改直播的头像
			$phone =M('ucenter_member')->where('id='.UID)->getField('username');
			$user_data = array(
				'avatar'	=>	$info['avator']['path'],
				'avatar_thumb'=>	$info['avator']['path']
			);
			M('users','cmf_')->where('user_login='.$phone)->save($data);
			$this->success("更新成功");
		}else{
			$this->error("更新失败");
		}
	}
	public function mine_page(){
        $config= M('config','cmf_')->where('id=1')->find();
        $data['apk_url'] = $config['apk_url'];
        $data['ipa_url'] = $config['ipa_url'];
        
    	$this->assign("wechat_no",C('WECHAT_NO'));
        $this->assign('data',$data);
		$this->display();
	}
	public function logout(){
		D('Member')->logout();
		$this->success("切换账号中……",U("Login/index"));
	}
}